<template>
   <div class="app-container">
      <el-row :gutter="24">
         <el-col :span="24" :xs="24">
            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
               <el-form-item label="房间号" prop="roomNumber">
                  <el-input
                     v-model="queryParams.roomNumber"
                     placeholder="请输入房间号"
                     clearable
                     style="width: 240px"
                     @keyup.enter="handleQuery"
                  />
               </el-form-item>
               <!-- <el-form-item label="房间面积" prop="area">
                  <el-input
                     v-model="queryParams.area"
                     placeholder="请输入房间面积"
                     clearable
                     style="width: 240px"
                     @keyup.enter="handleQuery"
                  />
               </el-form-item>
               <el-form-item label="房间状态" prop="status">
                  <el-select
                     v-model="queryParams.status"
                     placeholder="房间状态"
                     clearable
                     style="width: 240px"
                  >
                     <el-option
                        v-for="dict in room_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                     />
                  </el-select>
               </el-form-item> -->
               <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
               </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
               <el-col :span="1.5">
                  <el-button
                     type="primary"
                     plain
                     icon="Plus"
                     @click="handleAdd"
                     v-hasPermi="['serviceManage:room:add']"
                  >新增</el-button>
               </el-col>
               <el-col :span="1.5">
                  <el-button
                     type="success"
                     plain
                     icon="Edit"
                     :disabled="single"
                     @click="handleUpdate"
                     v-hasPermi="['serviceManage:room:update']"
                  >修改</el-button>
               </el-col>
               <el-col :span="1.5">
                  <el-button
                     type="danger"
                     plain
                     icon="Delete"
                     :disabled="multiple"
                     @click="handleDelete"
                     v-hasPermi="['serviceManage:room:remove']"
                  >删除</el-button>
               </el-col>
               <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
               <el-table-column type="selection" width="50" align="center" />
               <el-table-column label="房间号" align="center" key="roomNumber" prop="roomNumber" />
               <el-table-column label="房间面积" align="center" key="area" prop="area" :show-overflow-tooltip="true" />
               <el-table-column label="房间设施" align="center" key="facilities" prop="facilities" :show-overflow-tooltip="true" />
               <el-table-column label="VIP" align="center" key="isVip" prop="isVip" width="120">
                 <template #default="scope">
                    <dict-tag :options="is_vip" :value="scope.row.isVip" />
                  </template>
               </el-table-column>
               <el-table-column label="房间状态" align="center" key="status">
                  <template #default="scope">
                    <dict-tag :options="room_status" :value="scope.row.status" />
                  </template>
               </el-table-column>
               <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
                  <template #default="scope">
                     <el-tooltip content="修改" placement="top">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['serviceManage:room:update']"></el-button>
                     </el-tooltip>
                     <el-tooltip content="删除" placement="top">
                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['serviceManage:room:remove']"></el-button>
                     </el-tooltip>
                  </template>
               </el-table-column>
            </el-table>
            <pagination
               v-show="total > 0"
               :total="total"
               v-model:page="queryParams.current"
               v-model:limit="queryParams.size"
               @pagination="getList"
            />
         </el-col>
      </el-row>

      <!-- 添加或修改房间配置对话框 -->
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
         <el-form :model="form" :rules="rules" ref="userRef" label-width="80px">
            <el-row>
              <el-col :span="12">
                  <el-form-item label="房间号" prop="roomNumber">
                     <el-input v-model="form.roomNumber" placeholder="请输入房间号" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="房间面积" prop="area">
                     <el-input v-model="form.area" placeholder="请输入房间面积" />
                  </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="房间设施" prop="facilities">
                     <el-input v-model="form.facilities" placeholder="请输入房间设施" maxlength="50" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="VIP" prop="isVip">
                     <el-select v-model="form.isVip" placeholder="请选择是否VIP">
                        <el-option
                           v-for="dict in is_vip"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                        ></el-option>
                     </el-select>
                  </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="房间状态" prop="status">
                     <el-select v-model="form.status" placeholder="请选择房间状态">
                        <el-option
                           v-for="dict in room_status"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                        ></el-option>
                     </el-select>
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="User">
import {ref, reactive, getCurrentInstance, toRefs} from "vue";
import { useRouter } from "vue-router";

import { getRoomList, getRoomInfo, addRoomInfo, updateRoomInfo, delRoom, changeRoomStatus } from "@/api/serviceManage/room";

const router = useRouter();
const { proxy } = getCurrentInstance();
const { room_status, is_vip } = proxy.useDict("room_status", "is_vip");
const roomList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    roomNumber: undefined,
   //  area: undefined,
   //  status: undefined,
  },
  rules: {
    roomNumber: [{ required: true, message: "房间号不能为空", trigger: "blur" }],
    area: [{ required: true, message: "房间面积不能为空", trigger: "blur" }],
    facilities: [{ required: true, message: "房间设施不能为空", trigger: "blur" }],
    isVip: [{ required: true, message: "请选择该房间是否VIP", trigger: "change" }],
    status: [{ required: true, message: "请选择房间状态", trigger: "change" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户列表 */
function getList() {
  loading.value = true;
  getRoomList(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    loading.value = false;
    roomList.value = response.data.records;
    total.value = response.data.total;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const roomId = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除房间号为"' + roomId + '"的数据项？').then(function () {
    return delRoom(roomId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 重置操作表单 */
function reset() {
  form.value = {
    roomNumber: undefined,
   //  area: undefined,
   //  isVip: undefined,
   //  status: "1",
  };
  proxy.resetForm("userRef");
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加房间信息";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const roomId = row.id || ids.value;
  getRoomInfo(roomId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改房间信息";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["userRef"].validate(valid => {
    if (valid) {
      if (form.value.id !== undefined) {
        updateRoomInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRoomInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

getList();
</script>
